package br;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 * Servlet implementation class status
 */
@WebServlet("/status")
public class status extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public status() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 HttpSession sesja = request.getSession(true);
	 response.setCharacterEncoding("UTF-8");
	 response.setContentType("text/html;charset=UTF-8");
	 PrintWriter out = response.getWriter();
		System.out.println(sesja.getAttribute("log")+" "+sesja.getAttribute("nick"));
		if(sesja.getAttribute("log").equals("kurier")){ 
		 String nick = (String)sesja.getAttribute("nick");
		 String id = request.getParameter("idpaczki");
		 String status = request.getParameter("status");
		 Connection con =mb.commons.DbConnection.getConnection();
		  Statement stmt;
		  if(request.getParameter("dir").equals("do")){
		try {
			stmt = con.createStatement();
		 stmt.executeUpdate("Update zamowienia set status='"+status+"' Where id_zamowienia="+id);
		 if(status.equals("dostarczono"))
		 stmt.executeUpdate("Update zamowienia,paczki set paczki.data_dor=DATE(NOW()) Where id_zamowienia="+id+" AND paczki.id_paczki=zamowienia.id_paczki");
		 
		 
		 
		 out.println("Status zmieniony. <br><a href='trasa.jsp'>Powrot.</a>");
		 

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		  if(request.getParameter("dir").equals("od")){
				try {
					stmt = con.createStatement();
				 stmt.executeUpdate("Update zamowienia set status='"+status+"' Where id_zamowienia="+id);
				 if(status.equals("przyjeto"))
				 stmt.executeUpdate("Update zamowienia,paczki set paczki.data_dor=DATE(NOW()), paczki.oplata="+request.getParameter("zaplata")+" , paczki.pobranie="+request.getParameter("pobranie")+" Where id_zamowienia="+id+" AND paczki.id_paczki=zamowienia.id_paczki");
				 
				 
				 
				 out.println("Status zmieniony. <br><a href='trasa.jsp'>Powrot.</a>");
				 

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
	}
		

}
}
